package web.java.c_C3P0;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.Statement;

import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * C3P0组件
 * */

public class C3P0 {
	/**
	 * 硬编码方式
	 * 核心类：ComboPooledDataSource
	 * */
	@Test
	public void testName() throws Exception {
		ComboPooledDataSource dataSource=new ComboPooledDataSource();
		dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/shop");
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		dataSource.setUser("root");
		dataSource.setPassword("root");
		dataSource.setInitialPoolSize(3);
		dataSource.setMaxPoolSize(6);
		dataSource.setMaxIdleTime(3000);
		
		Connection conn=dataSource.getConnection();
		String sql="delete from emp where emp_id=26";
		Statement stmt=conn.createStatement();
		int result=stmt.executeUpdate(sql);
		System.out.println(result);
		conn.close();
	}
	/**
	 * 加载配置文件
	 * 核心类：
	 * */
	@Test
	public void testProps() throws Exception {
		//自动加载src目录下，c3p0-config.xml文件
		ComboPooledDataSource dataSource=new ComboPooledDataSource();
		Connection conn=dataSource.getConnection();
		String sql="delete from emp where emp_id=25";
		Statement stmt=conn.createStatement();
		int result=stmt.executeUpdate(sql);
		System.out.println(result);
		conn.close();
	}
}
